package com.anyou.common.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * redis工具类
 */
@Component
public class RedisUtil {

    @Autowired
    private  RedisTemplate redisTemplate;

    /**
     * 将值以指定要存储的时间存入redis
     * @param key 键
     * @param value 值
     * @param seconds 秒
     */
    public  void setValue(String key,String value,long seconds){
        redisTemplate.opsForValue().setIfAbsent(key,value,seconds, TimeUnit.SECONDS);
    }

    /**
     * 将值存入redis
     * @param key 键
     * @param value 值
     */
    public void setValue(String key,String value){
        redisTemplate.opsForValue().setIfAbsent(key,value);
    }

    /**
     * 从redis中获取key的值
     * @param key 键
     */
    public String getValue(String key){
        Object o = redisTemplate.opsForValue().get(key);
        if(o!=null){
            return o.toString();
        }
        return "";
    }

    /**
     * 从redis中删除key的值
     * @param key 键
     */
    public Boolean delValue(String key){
        Boolean delete = redisTemplate.delete(key);
        return delete;
    }
}
